﻿using PersistentObject.Common.DataConvertors;
using PersistentObject.Common.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace PersistentObject.Common.Attributes
{
    /// <summary>
    /// Define a property to use the specific IDataConvertor
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class ConvertorAttribute : Attribute
    {
        /// <summary>
        /// Indicate the IDataConvertor applying for the property
        /// </summary>
        public DataConvertor Convertor { get; private set; }
        /// <summary>
        /// The constructor of a Convertor attribute which requires a type of IDataConvertor
        /// </summary>
        /// <param name="DataConvertorType"></param>
        public ConvertorAttribute(Type DataConvertorType)
        {
            if (DataConvertorType == null)
            {
                throw new InvalidDataConvertorException("Data convertor type cannot be null value.");
            }
            else if (!typeof(DataConvertor).IsAssignableFrom(DataConvertorType))
            {
                throw new InvalidDataConvertorException(string.Format("{0} is not a class of data convertor.", DataConvertorType.Name));
            }
            else
            {
                Convertor = (DataConvertor)Activator.CreateInstance(DataConvertorType);
            }
        }
    }
}
